/**********************************************************
    COPYRIGHT
        KND, Inc. (c) 2011
    CLASS NAME
        platform_main.c
    DEPENDENCY

    HISTORY
		08/19/2011, created by Guangxing Liu
**********************************************************/

/************************INCLUDE**************************/
#include "platform_uart.h"
#include "platform_config.h"
#include "lpc177x_8x_timer.h"
#include "lpc177x_8x_clkpwr.h"
#include "lpc177x_8x_gpio.h"
#include "platform_timer.h"
#include "platform_gpio.h"
#ifdef WIN32_PLATFORM
#include <windows.h>

#endif
#include "lpc177x_8x.h"
#include "lpc_types.h"
#include "lpc177x_8x_mci.h"
#include "lpc177x_8x_gpdma.h"
#include "debug_frmwrk.h"
#include "lpc177x_8x_rtc.h"
#include "lpc177x_8x_clkpwr.h"
#include "lpc177x_8x_timer.h"
#include "bsp.h"
#include "diskio.h"
#include "ff.h"
#include "sdram_k4s561632j.h"	
#include "GUI_Private.h"
#include "BUTTON.h"
#include "GUIDEMO.h"

#include "GUI.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "MainTask.h"
#include "function.h"
#include "lpc177x_8x_wwdt.h"
#include "test_uip.h"

extern volatile platform_timer_counter uart0_timer;
extern volatile platform_timer_counter uart1_timer;
extern volatile platform_timer_counter uart2_timer;
extern volatile platform_timer_counter uart3_timer;

extern volatile platform_uart_buffer * uart0_recv_buffer;
extern volatile platform_uart_buffer * uart1_recv_buffer;
extern volatile platform_uart_buffer * uart2_recv_buffer;
extern volatile platform_uart_buffer * uart3_recv_buffer;

extern volatile platform_timer_counter adc_time_to_convert;
extern volatile unsigned char current_channel;
extern volatile unsigned char adc_wait_to_convert;

extern void *hyacinth_create(void);
extern hyacinth_service *hyacinth_get_service_handle(void *handle,const char *devname);
extern int  hyacinth_task_create(void *handle,unsigned char type,unsigned char priority,unsigned char is_permanent,void (*call_back)());
extern int  hyacinth_task_signal_box_push(void *handle,int src_task_id,int dest_task_id,void *data);
extern void*  hyacinth_task_signal_box_pop(void *handle,int *src_task_id,int dest_task_id);
extern void TC_Init(void);
extern void platform_init_uart_device(void);
extern void app_uart_recv_data(int task_id);
extern void platform_uart_init(void);
extern void platform_timer_init(void);
extern void platform_gpio_init(void);

extern void app_gpio_get_value(int task_id);
extern void app_I2C_read_data(int task_id);
#ifdef WIN32_PLATFORM
extern void CALLBACK TimerProc(HWND hwnd, UINT message, UINT timerID, DWORD time);
#endif

volatile FATFS fs;
volatile FIL fsrc;
volatile FRESULT res; 
volatile FILINFO bmpfile ,textfile;

volatile uint8_t test_text_buffer[18000];
uint8_t volatile Display_login[12000];
uint8_t char_wr_ptr[30054];



extern  page_t page[PAGE_SIZE];
extern char bBuffer[28];

extern volatile platform_timer_counter touch_time_to_convert;
extern volatile unsigned char g_app_touch_time_to_convert;

extern void Redraw_Callback(int pageid);
extern void handleSingnal_Callback(int pageid);


volatile unsigned char g_first_run =1;	

RTCTime RTC_Settime;

extern uip_ipaddr_t ipaddr;
extern struct timer periodic_timer, arp_timer;


//st_Wdt_Config wdtCfg;

/************************************************************************
	FUNCTION NAME : platform_init
	INPUTS: 
		none
	OUTPUTS:	
		none
	DESCRIPTION:	
		Platform init
************************************************************************/
void platform_init(void)
{
	platform_uart_init();
	platform_timer_init();
	platform_gpio_init();
	platform_pwm_init();
	platform_adc_init();
}


/************************************************************************
	FUNCTION NAME : main
	INPUTS: 
		int agrc,char* argv[]
	OUTPUTS:	
		none
	DESCRIPTION:	
		Main Function
************************************************************************/
int main(int agrc,char* argv[])
{

	#ifdef WIN32_PLATFORM
		MSG msg;
	#endif
	
	int ret = 0;
	int pageid = 1;
	const char *p0 ,*p1 ,*p2;
	volatile uint32_t br,filelen,text_filelen;
	volatile int flag = 1;
	RTC_TIME_Type *pFullTime;
	uint32_t delay;
	
	volatile unsigned char tick_cn=0;
	volatile unsigned int i=0;
	volatile void *handle = NULL;

	p0 = "0:pic/logo.bmp";
	p1 = "0:pic/login.bmp";
	p2 = "0:pic/test.txt";

	disk_initialize(0);

	Bsp_init();

	GUI_Init();

	RTC_Init(LPC_RTC);

//	RTC_Settime.RTC_Year = 2012;
//	RTC_Settime.RTC_Mon = 04;
//	RTC_Settime.RTC_Mday = 23;
//	RTC_Settime.RTC_Hour = 9;
//	RTC_Settime.RTC_Min = 40;
//	RTC_Settime.RTC_Sec = 50;
//	RTC_Settime.RTC_Wday = 1;
//	RTC_Settime.RTC_Yday = 114;
//	RTCSetTime(RTC_Settime);
//	RTCStart();
	
//	WWDT_Init(0x3fffff);
//
//	WWDT_SetMode(WWDT_RESET_MODE,ENABLE);
//
//	WWDT_Enable(ENABLE);
	f_mount(0, &fs);
	res=f_open(&fsrc, p0, FA_OPEN_EXISTING|FA_READ);
	if(res) 
	return FALSE;
	res=f_stat(p0, &bmpfile);
	if(res) return FALSE;
	filelen = bmpfile.fsize;
	f_read(&fsrc,char_wr_ptr,filelen,&br);

	res=f_open(&fsrc, p1, FA_OPEN_EXISTING|FA_READ);
	if(res) return FALSE;
	res=f_stat(p1, &bmpfile);
	if(res) return FALSE;
	filelen = bmpfile.fsize;
	f_read(&fsrc,Display_login,filelen,&br);

//	res=f_open(&fsrc, p2, FA_OPEN_EXISTING|FA_READ| FA_WRITE);
//	if(res) 
//	return FALSE;
//	f_stat(p2, &textfile);
//	text_filelen = textfile.fsize;
//	res = f_read(&fsrc, &staff_info[0], text_filelen, &br);
//	if(res) 
//	return FALSE;

	res=f_open(&fsrc, p2, FA_OPEN_EXISTING|FA_READ |FA_WRITE);
	if(res) return FALSE;
	res=f_stat(p2, &textfile);
	if(res) return FALSE;
	text_filelen = textfile.fsize;
	f_read(&fsrc,test_text_buffer,text_filelen,&br);  

	f_close(&fsrc);	
	f_mount(0, NULL);

//  test  uip

	debug_frmwrk_init();

	_DBG_("Hello NXP Semiconductors");
	_DBG_("uIP porting on LPC17xx");

	_DBG_("Init Clock");
	// Sys timer init 1/100 sec tick
//	clock_init();
//
//	timer_set(&periodic_timer, CLOCK_SECOND / 2); /*0.5s */
//	timer_set(&arp_timer, CLOCK_SECOND * 10);	/*10s */
//
//	_DBG_("Init EMAC");
//	// Initialize the ethernet device driver
//	while(!tapdev_init()){
//		// Delay for a while then continue initializing EMAC module
//		_DBG_("Error during initializing EMAC, restart after a while");
//		for (delay = 0x100000; delay; delay--);
//	}

//	uip_test_init();
//end test uip
	
	GPIO_SetDir(1,1<<12,GPIO_DIRECTION_OUTPUT);
	
	GPIO_SetDir(1,1<<13,GPIO_DIRECTION_OUTPUT);

	platform_init();

	platform_init_uart_device();//Install uart device
	
	platform_init_gpio_device();//Install gpio device

	platform_init_adc_device();//Install adc device

	platform_init_pwm_device();//Install adc device
	
 	handle = hyacinth_create();

	app_uart_init(handle);

	app_I2C_init(handle);

	app_touch_init(handle);

 	InitPageInformation(page);

	touch_init();

	TC_Init();//init timer0

//	for(tick_cn=0;tick_cn<10;tick_cn++)
//	{
//		for(i=0;i<2000000;i++);
//
//		if(tick_cn%2==0)
//		{
//			GPIO_OutputValue(5,1<<3,1);
//		}else
//		{
//			GPIO_OutputValue(5,1<<3,0);
//		}
//	}
//	GUIDEMO_Touch();
	_ExecCalibration();

//	g_first_run =0;
	
	#ifdef WIN32_PLATFORM
		SetTimer(NULL, 0, 500, TimerProc);
	#endif
	

	//hyacinth_task_create(handle, TASK_TYPE_CALLBACK, TASK_PRIORITY_NORMAL, TRUE, app_uart_recv_data);
	//hyacinth_task_create(handle, TASK_TYPE_CALLBACK, TASK_PRIORITY_NORMAL, TRUE, app_gpio_get_value);
	//hyacinth_task_create(handle, TASK_TYPE_CALLBACK, TASK_PRIORITY_NORMAL, TRUE, app_I2C_read_data);


	#ifdef WIN32_PLATFORM
		SetTimer(NULL, 0, 500, TimerProc);
	#endif

	while(1)
	{ 	

//		WWDT_FeedStdSeq();
		hyacinth_task_run(handle);
//		uip_connect(uip_ipaddr_t * ripaddr, u16_t rport);

		if(flag)
		{
			ret = Redraw(pageid, Redraw_Callback);			
		}
		
		ret = handleSingnal(pageid, handleSingnal_Callback);
		if(ret == pageid)
		{
		 	flag = 0;
		}

		if(ret != pageid)
		{
			destroypage(pageid);
			pageid = ret;
			flag = 1;
		}
		
		#ifdef WIN32_PLATFORM
			if(GetMessage(&msg, NULL, 0, 0))
			{
				  TranslateMessage(&msg);
	 			  DispatchMessage(&msg);
			}
		#endif
		
	}

//	uip_test();
	
	return 0;
}

#ifdef WIN32_PLATFORM
void CALLBACK TimerProc(HWND hwnd, UINT message, UINT timerID, DWORD time)
{

 	platform_printf("Checking For Uart  Interface... \n");
	platform_printf("Checking For PWM Interface... \n");
	platform_printf("Checking For USB Interface... \n");
	platform_printf("Checking For CAN Interface... \n");
	platform_printf("Checking For NETWORK Interface... \n");
}
#endif


void TIMER0_IRQHandler(void)
{
	volatile static unsigned char tickcn=0;

	if(TIM_GetIntStatus(LPC_TIM0, TIM_MR0_INT) !=SET) return;

	platform_uart_send_data(PORT_UART0);

	platform_uart_recv_data(PORT_UART0);

	platform_uart_timer_counter(&uart0_timer,uart0_recv_buffer);


	switch(tickcn)
	{																					    
		case 0:
			if(adc_time_to_convert.start)
			{
				if(++adc_time_to_convert.counter >= adc_time_to_convert.counter_timeout)
				{
					adc_time_to_convert.counter = 0;
					
					adc_time_to_convert.start =0;
					
					app_adc_begin_to_convation(current_channel);
					
					adc_wait_to_convert =1;
							
				}
			}
			break;
		case 1:
			if(adc_wait_to_convert)
			{						     
				app_adc_time_to_get_value(current_channel);
			}
			break;
		case 2:
			if(touch_time_to_convert.start)
			{
				if(++touch_time_to_convert.counter >= touch_time_to_convert.counter_timeout)
				{
					touch_time_to_convert.counter =0;

					if(g_first_run)
					{
						GUI_TOUCH_Exec();
					}
					else
					{
						g_app_touch_time_to_convert =1;
//						rtc_time_get = RTC_FullTime();
//						BinaryToAscii(rtc_time_get,Local_time); 
//						GUI_SetFont(&GUI_Font16_ASCII);
//						GUI_DispStringAt(Local_time, 650, 10);
					}
					
				}
			}
			
			break;
		case 3:
			break;
		case 4:
			break;
		case 5:
			break;
		case 6:
			break;
		case 7:
			break;
		default:
			break;
	}

	if(++tickcn>=8) tickcn=0;

	TIM_ClearIntPending(LPC_TIM0,TIM_MR0_INT);

}

























